package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.dao.business.domain.User;
import org.dromara.dao.business.domain.UserTypeSetting;
import org.dromara.dao.business.enums.UserTypeEnums;
import org.dromara.dao.business.mapper.UserMapper;
import org.dromara.dao.business.mapper.UserTypeSettingMapper;
import org.dromara.web.plugins.telegram.TelegramBotPluginsV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Service
public class UDunService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;
    @Autowired
    UserTypeSettingMapper userTypeSettingMapper;


    public void udunRechargeNotify(String address, BigDecimal money) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUdunAddressHigh, address).or().eq(User::getUdunAddressLow, address));
        if (user == null) {
            return;
        }



        // lowAddress 是升级会员
        // highAddress 是充值套餐
        if (address.equals(user.getUdunAddressLow())) {
            UserTypeSetting userTypeSetting = userTypeSettingMapper.selectOne(new LambdaQueryWrapper<UserTypeSetting>().eq(UserTypeSetting::getMonthFee, money).or().eq(UserTypeSetting::getYearFee, money));
            if (userTypeSetting == null) {
                throw new ServiceException("设置有误");
            }
            if (userTypeSetting.getUserType() == UserTypeEnums.USER_TYPE_MOBILE) {
                if (money.compareTo(userTypeSetting.getMonthFee()) == 0) {
                    // 开通移动端月套餐
                    user.setUserType(UserTypeEnums.USER_TYPE_MOBILE);
                    user.setUserTypeEnd(user.getUserTypeEnd() == null ? LocalDateTime.now().plusMonths(1) : user.getUserTypeEnd().plusMonths(1));

                } else {
                    // 开通移动年套餐
                    user.setUserType(UserTypeEnums.USER_TYPE_MOBILE);
                    user.setUserTypeEnd(user.getUserTypeEnd() == null ? LocalDateTime.now().plusYears(1) : user.getUserTypeEnd().plusYears(1));

                }
            } else {
                if (money.compareTo(userTypeSetting.getMonthFee()) == 0) {
                    // 开通全端月套餐
                    user.setUserType(UserTypeEnums.USER_TYPE_ALL);
                    user.setUserTypeEnd(user.getUserTypeEnd() == null ? LocalDateTime.now().plusMonths(1) : user.getUserTypeEnd().plusMonths(1));

                } else {
                    // 开通全端年套餐
                    user.setUserType(UserTypeEnums.USER_TYPE_ALL);
                    user.setUserTypeEnd(user.getUserTypeEnd() == null ? LocalDateTime.now().plusYears(1) : user.getUserTypeEnd().plusYears(1));
                }
            }
            user.setUserTypeStart(user.getUserTypeStart() == null ? LocalDateTime.now() : user.getUserTypeStart());
            userMapper.updateById(user);
        } else {
            TelegramBotPluginsV2.TelegramGradePackage[] packages = TelegramBotPluginsV2.TelegramGradePackage.values();
            TelegramBotPluginsV2.TelegramGradePackage grade = null;
            for(TelegramBotPluginsV2.TelegramGradePackage pkg : packages) {
                if (pkg.getAmount().compareTo(money) == 0) {
                    grade = pkg;
                    break;
                }
            }
            if (grade == null) {
                throw new ServiceException("套餐有误");
            }
            userService.userBalanceChange(user.getId(), 0, grade.getGrade() , "U盾充值到账");
        }




    }

}
